// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const voiceCollection = db.collection('voice')
const likeCollection = db.collection('liker_liked')
const cmtCollection = db.collection('comment')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const voiceId = event.voiceId
  let message = 0
  let img = ''
  await voiceCollection.doc(voiceId).get().then(res => {
    img = res.data.coverImg
  })
  if (img != '') {
    await cloud.deleteFile({
      fileList: [img],
    })
  }
  await likeCollection.where({
    likedVoiceId: voiceId
  }).remove()

  await cmtCollection.where({
    voiceId: voiceId
  }).remove()

  await voiceCollection.doc(voiceId).remove().then(res => {
    message = res.stats.removed
  })
  
  return message
}